<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>在线心理测评管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../layuiadmin/layui/css/layui.css" th:href="@{/layuiadmin/layui/css/layui.css}"
          media="all">
    <link rel="stylesheet" href="../layuiadmin/style/admin.css" th:href="@{/layuiadmin/style/admin.css}" media="all">
</head>
<body class="layui-layout-body">
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <form class="layui-form" action="" method="post">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">公告标题</label>
                        <div class="layui-input-inline">
                            <input type="text" id="titles" name="titles" placeholder="请输入" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">操作用户</label>
                        <div class="layui-input-inline">
                            <input type="text" id="userOps" name="userOps" placeholder="请输入" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn" lay-submit lay-filter="queryForm"
                                style="margin-left: 120px">立即查询
                        </button>
                    </div>
                </div>
            </form>
        </div>
        <!--表格-->
        <table id="notice-list" lay-filter="notice-list"></table>
        <script type="text/html" id="table-useradmin-webuser">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="look">
                <i class="layui-icon layui-icon-read"></i>查看</a>
            <a class="layui-btn layui-btn-xs" lay-event="edit">
                <i class="layui-icon layui-icon-edit"></i>编辑</a>
            </a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">
                <i class="layui-icon layui-icon-delete"></i>删除</a>
            </a>
        </script>
    </div>
</div>
<!--隐藏模块-->
<div class="layui-row" id="popUpdateNotice" style="display:none;">
    <form class="layui-form" action="" style="margin-top:20px">
        <div style="padding: 20px; background-color: #F2F2F2;">
            <div class="layui-row layui-col-space15">
                <div class="layui-card-header">请填写公告信息</div>
                <div class="layui-card-body">
                    <div class="layui-form-item">
                        <label class="layui-form-label">公告ID</label>
                        <div class="layui-input-inline">
                            <input type="text" id="id" name="id" readonly lay-verify="required" placeholder="请输入用户名"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">公告标题</label>
                        <div class="layui-input-inline">
                            <input type="text" id="title" name="title" lay-verify="required" placeholder="请输入用户名"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">公告内容</label>
                        <div class="layui-input-inline">
                            <textarea id="content" name="content" lay-verify="required"
                                      style="width: 400px; height: 150px;" autocomplete="off"
                                      class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <button class="layui-btn" lay-submit lay-filter="updateForm"
                style="margin-left: 120px">立即提交
        </button>
    </form>
</div>

<div class="layui-row" id="popUpdateNoticeLook" style="display:none;">
    <div style="padding: 20px; background-color: #F2F2F2;">
        <div class="layui-row layui-col-space15">
            <div class="layui-card-body">
                <div class="layui-form-item">
                    <label class="layui-form-label">公告ID</label>
                    <div class="layui-input-inline">
                        <input type="text" id="id1" name="id" readonly lay-verify="required" placeholder="请输入用户名"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">公告标题</label>
                    <div class="layui-input-inline">
                        <input type="text" id="title1" name="title" lay-verify="required" placeholder="请输入用户名"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">公告内容</label>
                    <div class="layui-input-inline">
                            <textarea id="content1" name="content" lay-verify="required"
                                      style="width: 400px; height: 150px;" autocomplete="off"
                                      class="layui-textarea"></textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
</div>
<script src="../layuiadmin/layui/layui.js" th:src="@{/layuiadmin/layui/layui.js}"></script>
<script>
    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use('index');
</script>
<script>
    layui.use(['table', 'form'], function (exports) {
        var $ = layui.$, table = layui.table, form = layui.form;

        //用户管理
        table.render({
            elem: '#notice-list'
            , url: '/admin/notice/list' //
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'id', width: 100, title: '公告ID', sort: true}
                    , {field: 'title', title: '公告标题', width: 280}
                    , {field: 'createTime', title: '发布时间', width: 180}
                    , {field: 'userOp', title: '操作用户', width: 180}
                    , {title: '操作', width: 250, align: 'center', fixed: 'right', toolbar: '#table-useradmin-webuser'}
                ]
            ]
            , page: true
            , limit: 10
            , height: 'full-220'
            , text: '对不起，加载出现异常！'
        });

        //监听查询点击事件
        form.on('submit(queryForm)', function (data) {
            table.render({
                elem: '#notice-list'
                , height: 312
                , url: '/admin/notice/list?keyword1=' + data.field.titles + '&keyword2=' + data.field.userOps //后台数据接口
                , cols: [
                    [
                        {type: 'checkbox', fixed: 'left'}
                        , {field: 'id', width: 100, title: '公告ID', sort: true}
                        , {field: 'title', title: '公告标题', width: 280}
                        , {field: 'createTime', title: '发布时间', width: 180}
                        , {field: 'userOp', title: '操作用户', width: 180}
                        , {
                        title: '操作',
                        width: 250,
                        align: 'center',
                        fixed: 'right',
                        toolbar: '#table-useradmin-webuser'
                    }
                    ]
                ], page: true
                , limit: 10
                , height: 'full-220'
                , text: '对不起，加载出现异常！'
            });
            return false;
        });
        //监听行工具事件
        table.on('tool(notice-list)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除公告吗！', function (index) {

                    //异步像服务器发送删除请求
                    $.ajax({
                        url: '/admin/notice/del',
                        type: 'GET',
                        data: {'id': data.id},
                        success: function (result) {
                            if (result.success = true) {
                                layer.msg("删除成功!" + result, {icon: 6});
                                obj.del();
                                layer.close(index);

                            } else {
                                layer.msg("删除失败!" + result, {icon: 5});
                                layer.close(index);
                            }
                        },
                        error: function (errorMsg) {
                            layer.msg("数据异常!" + errorMsg, {icon: 5});
                            layer.close(index);
                            location.reload();
                        },
                    });
                });
                //更新用户
            } else if (obj.event == 'look') {
                layer.open({
                    type: 1,
                    title: "查看公告",
                    area: ['600px', '500px'],
                    content: $("#popUpdateNoticeLook"),
                    success: function () {
                        //回显数据
                        $("#id1").val(data.id);
                        $("#title1").val(data.title);
                        $("#content1").val(data.content);
                    },
                });

            } else if (obj.event == 'edit') {
                layer.open({
                    type: 1,
                    title: "更新公告",
                    area: ['600px', '500px'],
                    content: $("#popUpdateNotice"),
                    success: function () {
                        //回显数据
                        $("#id").val(data.id);
                        $("#title").val(data.title);
                        $("#content").val(data.content);
                    },
                });
            }
        });
        //更新操作
        form.on('submit(updateForm)', function (data) {
            console.log(data);
            //发送ajax请求
            $.ajax({
                url: '/admin/notice/update',
                data: JSON.stringify(data.field),
                type: 'POST',
                contentType: 'application/json',  //数据类型格式
                success: function (result) {
                    if (result.success = true) {
                        layer.closeAll();
                        layer.msg('更新成功', {time: 1 * 1000}, function () {
                            location.reload();
                        });
                    } else {
                        layer.closeAll();
                        layer.msg('更新失败', {time: 1 * 1000}, function () {
                            location.reload();
                        });
                    }
                },
                error: function (errorMsg) {
                    alert("数据异常！" + errorMsg);
                    location.reload();
                },
            });
            return false;
        });
    });
</script>
</body>
</html>